//
//  SleepViewController.swift
//  MoYoung
//
//  Created by sylar on 16/10/14.
//  Copyright © 2016年 sylar. All rights reserved.
//

import UIKit
import JXSegmentedView
import RxSwift
import RxCocoa
import RxGesture
import SwiftDate
import MYModule

public class SleepViewController: BaseTableViewController {
    
    var panGesture: Driver<Void>?
    
    public var revealAll: Bool = true
    
    public var viewModel = SleepViewModel()
    
    public var provider: SleepProvider?
    
    /// 是否显示right item
    public var showRightItem: Bool = true
    
    
    public override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func makeNavigationBar() {
        
//        self.navigation.bar.isTranslucent = true
//        self.navigation.bar.alpha = 1.0
        self.navigation.bar.backgroundColor = UIColor.background
        self.navigation.bar.tintColor = UIColor.assist_222222
        
        
        self.navigation.bar.titleTextAttributes = [.foregroundColor:UIColor.assist_222222,
                                                   .font:Fonts.boldSystemFont(ofSize: 20)]
        
        
        if revealAll && self == self.navigationController?.children.first {
            
            let titleLabl = Label()
            titleLabl.textColor = .assist_222222
            titleLabl.font = Fonts.systemFont(ofSize: 20)
            titleLabl.text = "sleep_title".language
            
//            let longTap = UILongPressGestureRecognizer(target: self, action: #selector(debugAction(long:)))
//            titleLabl.isUserInteractionEnabled = true
//            titleLabl.addGestureRecognizer(longTap)
            
            self.navigation.item.leftBarButtonItem = BarButtonItem(customView: titleLabl)
            
            
        } else {
            
            self.navigation.item.title = "sleep_title".language
            let closeBtn = Button(type: .custom)
            closeBtn.setImage(UIImage(named: "ic_back")?.originalImageWithColor(color: UIColor.assist_222222), for: .normal)
            self.navigation.item.leftBarButtonItem = BarButtonItem(customView: closeBtn)
            
            closeBtn.rx.tap.subscribe(onNext:{ [weak self]  in
                
                self?.navigationController?.popViewController(animated: true)
                
            }).disposed(by: rx.disposeBag)
        }
        
        
        if showRightItem {
            
            let historyBtn = Button(type: .custom)
            historyBtn.setImage(UIImage(named: "ic_date")?.originalImageWithColor(color: UIColor.assist_222222), for: .normal)
            self.navigation.item.rightBarButtonItem = BarButtonItem(customView: historyBtn)
            
            
            historyBtn.rx.tap.subscribe(onNext:{ [weak self]  in
                
                guard let `self` = self else {return}
                
                let vc = CalendarController()
                vc.showType = .sleep24h
                vc.revealAll = self.revealAll
              
                self.navigationController?.pushViewController(vc, animated: true)
                
            }).disposed(by: rx.disposeBag)
            
        }
        
    
    }
    
//    @objc func debugAction(long: UILongPressGestureRecognizer) {
//
//        if long.state == .ended {
//
//            let log = LogController()
//            self.navigationController?.pushViewController(log, animated: true)
//
//        }
//
//    }
    
    public override func makeUI() {
        super.makeUI()
        
        makeNavigationBar()
        
        self.view.backgroundColor =  UIColor.darkBackground
        
        tableView.bounces = false
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            
            make.left.right.equalToSuperview()
            if revealAll {
                if #available(iOS 11.0, *) {
                    make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
                    make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
                } else {
                    
                    if let navigationBar = self.navigationController?.navigationBar {
                        make.top.equalTo(navigationBar.snp.bottom)
                    }
                    
                    guard let tabBar = self.tabBarController?.tabBar else { return }
                    make.bottom.equalTo(tabBar.snp.top)
                }
            } else {
                make.top.equalToSuperview()
                make.bottom.equalToSuperview()
            }
        }
        
    }
    
    public override func bindViewModel() {
        super.bindViewModel()
        
        var input = SleepViewModel.Input(revealAll: revealAll, viewWillAppear: rx.viewWillAppear)
        
        if let provider = provider {
            input.provider = provider
        }
    
        let output = self.viewModel.transform(input: input)
        
        let cellTypes = output.dataSource.value
        
        cellTypes.forEach {
            
            self.tableView.register($0.cellClass, forCellReuseIdentifier: $0.indentifier)
            
        }
        
        // 可能会重复绑定，所以绑定前清空上一次绑定
        tableView.dataSource = nil
        
        output.dataSource.bind(to: tableView.rx.items) {[weak self] tableView, row, cellType in
            
            let indexPath = IndexPath(row: row, section: 0)
            
            let cell = tableView.dequeueReusableCell(withIdentifier: cellType.indentifier, for: indexPath) as! SleepBaseCell
            
            cell.bind(to: cellType)
            
            if let cell = cell as? SleepHearRateCell, let weakSelf = self {
                cell.learnMoreHandle = {[weak weakSelf] in
                    weakSelf?.pushtoHeartPatternsController()
                }
            }
            
            return cell
            
        }.disposed(by: rx.disposeBag)
    }
    
    func pushtoHeartPatternsController() {
        
        let vc = SleepHeartPatternsController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}

